package com.cskaoyan.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.user.AdminUserInfoBO;
import com.cskaoyan.bean.bo.user.AdminUserListBO;
import com.cskaoyan.bean.po.user.AdminUserListPO;
import com.cskaoyan.bean.vo.user.AdminUserListVO;
import com.cskaoyan.mapper.user.AdminUserMapper;
import com.cskaoyan.mapper.user.UpdateAdminUserMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/18 10:45
 */
@Service
@Data
public class AdminUserServiceImpl implements AdminUserService {
    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<AdminUserListPO> list;
    @Autowired
    AdminUserMapper adminUserMapper;

    @Autowired
    UpdateAdminUserMapper updateAdminUserMapper;

    @Override
    public AdminUserListVO selectAllAdminUser(AdminUserListBO adminUserListBO) {
        this.page = adminUserListBO.getPage();
        limit = adminUserListBO.getLimit();
        String username = adminUserListBO.getUsername();
        String mobile = adminUserListBO.getMobile();

        PageHelper.startPage(page, limit);

        if (StringUtils.isEmpty(username) && StringUtils.isEmpty(mobile)) {
            list = adminUserMapper.selectList(null);

        } else {
            QueryWrapper<AdminUserListPO> adminUserListPOQueryWrapper = new QueryWrapper<>();

            adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(username), "username", username);
            adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(mobile), "mobile", mobile);
            list = adminUserMapper.selectList(adminUserListPOQueryWrapper);
        }

        PageInfo<AdminUserListPO> adminUserListPOPageInfo = new PageInfo<>(list);

        this.total = (int) adminUserListPOPageInfo.getTotal();
        pages = adminUserListPOPageInfo.getPages();
        AdminUserListVO adminUserListVO = new AdminUserListVO();
        BeanUtil.copyBean(this, adminUserListVO);

        return adminUserListVO;
    }

    @Override
    public AdminUserListPO selectAdminUserById(Integer id) {
        return adminUserMapper.selectById(id);
    }

    @Override
    public Integer updateAdminUserInfo(AdminUserInfoBO adminUserInfoBO) {
        Integer id = adminUserInfoBO.getId();
        String password = adminUserInfoBO.getPassword();
        String mobile = adminUserInfoBO.getMobile();

        if (StringUtils.isEmpty(password)) {
            return 0;
        }
        if (!org.apache.commons.lang3.StringUtils.isNumeric(mobile)) {
            return 0;
        }
        UpdateWrapper<AdminUserInfoBO> adminUserInfoBOUpdateWrapper = new UpdateWrapper<>();
        adminUserInfoBOUpdateWrapper.eq("id", id);
        return updateAdminUserMapper.update(adminUserInfoBO, adminUserInfoBOUpdateWrapper);

    }
}
